import numpy as np
import math
import matplotlib.pyplot as plt
from RA_Cal import csvtest

import os
parent_directory_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(parent_directory_path)

xdata_1, ydata_1 = csvtest.Read_csv(parent_directory_path+"\RA_Cal\Laba_real.csv", 1)
maxy = max(ydata_1)
fxt_grid = np.zeros((361, 361))
for i in range(361):
    for j in range(361):
        fxt_grid[j, i] = ydata_1[i * 361 + j]

mag_ = np.ones(16)
ang_ = np.array([-27.3, -38.4, -53.0, -69.3, -90.0, -112.4, -137.0, -159.6,
                 -180.8, -202.4, -222.7, -242.8, -268.9, -296.4, -325.0, -356.8]) / 180 * math.pi

def degree_to_rad(d):
    return d/180*math.pi

# 双线性拟合
def sxxcz(Q11, Q21, Q12, Q22, bx, by):
    fxy1 = (1 - bx) * Q11 + bx * Q21
    fxy2 = (1 - bx) * Q12 + bx * Q22
    fxy = (1 - by) * fxy1 + by * fxy2
    return fxy

# 单元入射方向图
def RP_ele(the, phi):
    qe = 1
    return abs((math.cos(the))) ** qe

def RP_qf(the, phi):
    qe = 5.4
    return abs((math.cos(the))) ** qe

# 喇叭方向图
def RP_LABA(the, phi):
    the_d = round((the * 180 / math.pi) % 360, 2)
    phi_d = round((phi * 180 / math.pi) % 360, 2)
    the_a = int(the_d)
    the_b = the_d - the_a
    phi_a = int(phi_d)
    phi_b = phi_d - phi_a

    qq = sxxcz(fxt_grid[the_a, phi_a], fxt_grid[the_a + 1, phi_a], fxt_grid[the_a, phi_a+1],
                   fxt_grid[the_a + 1, phi_a + 1], the_b, phi_b)
    fians = 1 / (10 ** ((maxy - qq) / 20))
    return fians

if __name__ == '__main__':

    # ppt = np.linspace(degree_to_rad(-50), degree_to_rad(49.9), 1000)
    # p0deg = []
    # p30deg = []
    # p60deg = []
    # p90deg = []
    # for i in ppt:
    #     p0deg.append(PHASE_LABA(i, degree_to_rad(30)))
    #     p30deg.append(PHASE_LABA(i, degree_to_rad(90)))
    #     p60deg.append(PHASE_LABA(i, degree_to_rad(150)))
    #     p90deg.append(PHASE_LABA(i, math.pi/2))
    #
    # plt.plot(ppt, p0deg)
    # plt.plot(ppt, p60deg)
    #
    #
    # plt.grid()
    # plt.show()

    ppt = np.linspace(degree_to_rad(-90), degree_to_rad(90), 1000)
    g0deg = []
    g30deg = []
    g60deg = []
    g90deg = []
    for i in ppt:
        g0deg.append(RP_LABA(i, 0))
        g30deg.append(RP_LABA(i, degree_to_rad(45)))
        g60deg.append(RP_qf(i, 0))
        g90deg.append(RP_LABA(i, math.pi/2))
    plt.plot(ppt, g0deg, ppt, g30deg, ppt, g60deg, ppt, g90deg)
    plt.legend(["0","30","qf","90"])
    plt.grid()
    plt.show()